package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCompanyAlreadyPublishBinding;
import com.sgb.kjwl.databinding.FragmentPropertyAlreadyPublishBinding;
import com.sgb.kjwl.model.entity.enterprise.PublishPatentDetailEntity;
import com.sgb.kjwl.model.entity.enterprise.PublishPropertyDetailEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverPublishActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.ImageAdapter;
import com.sgb.kjwl.viewmodel.enterprise.PublishViewModel;

import java.util.Arrays;

public class PropertyAlreadyPublishFragment extends AppFragment {

    FragmentPropertyAlreadyPublishBinding mBinding;
    PublishViewModel mViewModel;
    PublishPropertyDetailEntity mEntity;
    private ImageAdapter imageAdapter;

    public static PropertyAlreadyPublishFragment newInstance(long id){
        PropertyAlreadyPublishFragment tradePublishFragment = new PropertyAlreadyPublishFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("id",id);
        tradePublishFragment.setArguments(bundle);
        return tradePublishFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_property_already_publish,container,false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mBinding.setLifecycleOwner(this);
        mViewModel = new ViewModelProvider(this).get(PublishViewModel.class);
        long id = getArguments().getLong("id");
        mBinding.setViewModel(mViewModel);
        mBinding.setContext(getContext());
        mBinding.tvEnable.setOnClickListener(this);
        mBinding.tvOperateDown.setOnClickListener(this);
        mBinding.tvDelete.setOnClickListener(this);

        imageAdapter = new ImageAdapter(getContext(),null);
        mBinding.ivFragmentPropertyPublishUploadPicture.setLayoutManager(new GridLayoutManager(getContext(),4));
        mBinding.ivFragmentPropertyPublishUploadPicture.setAdapter(imageAdapter);
        mViewModel.getPropertyPublishDetail(getContext(),id+"",5);
        mViewModel.mJump.observe(this.getViewLifecycleOwner(),integer -> {
            if(integer==5){
                ActivityJump.jumpToActivityForResult(getActivity(), MakeOverPublishActivity.class,1, Pair.create("type", integer), Pair.create("entity", mEntity));
            }
        });

        mViewModel.mDeleteSecess_Type.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                getActivity().finish();;
            }
        });

        mViewModel.mPublishPropertyDetailLiveData.observe(getViewLifecycleOwner(), new Observer<PublishPropertyDetailEntity>() {
            @Override
            public void onChanged(PublishPropertyDetailEntity publishPropertyDetailEntity) {
                mBinding.idRootLayout.setVisibility(View.VISIBLE);
                mBinding.setC(publishPropertyDetailEntity);
                mEntity = publishPropertyDetailEntity;
                imageAdapter.setDatas(Arrays.asList(publishPropertyDetailEntity.getImages(publishPropertyDetailEntity.getCommodityImages())));
                if (TextUtils.isEmpty(publishPropertyDetailEntity.getAssetsOldNew())) {
                    mBinding.tvFragmentTradePublishSelectNewOrOld.setVisibility(View.GONE);
                    mBinding.tvFragmentPropertyPublishNewOrOldTags.setVisibility(View.GONE);
                    mBinding.ivFragmentPropertyPublishLine3.setVisibility(View.GONE);
                } else {
                    mBinding.tvFragmentTradePublishSelectNewOrOld.setVisibility(View.VISIBLE);
                    mBinding.tvFragmentPropertyPublishNewOrOldTags.setVisibility(View.VISIBLE);
                    mBinding.ivFragmentPropertyPublishLine3.setVisibility(View.VISIBLE);
                }

               mBinding.etFragmentPropertyPublishBuyPriceEdit.setText(publishPropertyDetailEntity.getBuyingPrice() == -2?"/":
                       PointUtils.price(publishPropertyDetailEntity.getBuyingPrice())+"元");
                mBinding.etFragmentPropertyPublishMakeoverMuchEdit.setText(publishPropertyDetailEntity.getSalePrice() == -1?"议价":
                        PointUtils.price(publishPropertyDetailEntity.getSalePrice())+"元");

                String content = mEntity.getDetailedDescription();
                if (content != null && !content.equals("")) {
                    mBinding.webDescription.getSettings().setDefaultTextEncodingName("UTF-8");
                    mBinding.webDescription.getSettings().setJavaScriptEnabled(true);//支持JS
                    String js = "<script type=\"text/javascript\">" +
                            "var imgs = document.getElementsByTagName('img');" + // 找到img标签
                            "for(var i = 0; i<imgs.length; i++){" +  // 逐个改变
                            "imgs[i].style.width = '100%';" +  // 宽度改为100%
                            "imgs[i].style.height = 'auto';" +
                            "}" +
                            "</script>";
                    mBinding.webDescription.loadDataWithBaseURL(null, content + js, "text/html", "UTF-8", null);
                    mBinding.webDescription.setVisibility(View.VISIBLE);
                    mBinding.emptyView.setVisibility(View.GONE);
                } else {
                    mBinding.webDescription.setVisibility(View.GONE);
                    mBinding.emptyView.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_enable://上架
                if (mEntity != null) {
                    DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认上架？", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            mViewModel.changeStateMakeoverPublish(getContext(), mEntity.getId(), 5, "1");
                        }
                    });
                }
                break;
            case R.id.tv_operate_down://下架
                if (mEntity != null) {
                    DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认下架？", "确定", new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            mViewModel.changeStateMakeoverPublish(getContext(), mEntity.getId(), 5, "-1");
                        }
                    });
                }
                break;
            case R.id.tv_delete://删除
                DialogHelper.showDialog(getActivity(), "温馨提示", "是否确认删除？", "确定", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        mViewModel.deleteMakeoverPublish(getContext(), mEntity.getId() + "", 5);
                    }
                });
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0){
            getActivity().finish();
        }
    }
}
